package com.example.heathy_me.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;

import com.example.heathy_me.MyApplication;
import com.example.heathy_me.R;
import com.example.heathy_me.homeDetail.Medicine;
import com.example.heathy_me.homeDetail.Medicine_List;
import com.example.heathy_me.homeDetail.Report;
import com.example.heathy_me.homeDetail.Report_List;
import com.example.heathy_me.utils.MedicineList;
import com.example.heathy_me.utils.RequestHelper;
import com.example.heathy_me.utils.Waring;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Home extends Fragment {
    private String[] mStrs = {"kk", "kk", "wskx", "wksx"};
    private SearchView mSearchView;
    private ListView lListView;
    private Button test_button;
    private ImageView camera;
    private String Url = "http://192.168.0.106:5000/user?username=test&password=test";
    private ListView medicineList;
    private RequestHelper req = new RequestHelper();
    private List<Waring> medicinelist;
    MyApplication app;
    private List<Map<String,String>> list_map = new ArrayList<Map<String,String>>();
        @Override
        public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.home,container,false);
        }


        @Override
        public void onActivityCreated( Bundle savedInstanceState) {

            super.onActivityCreated(savedInstanceState);
            //药物禁忌跳转
            app = (MyApplication)getActivity().getApplication();
            ImageView imageview= getActivity().findViewById(R.id.medicineIcon);
            medicineList = getActivity().findViewById(R.id.medicineList);
            try {
                setList();
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(getActivity(), Medicine.class);
                    startActivity(intent);
                }
            });

            ImageView history= getActivity().findViewById(R.id.reportIcon);

            history.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(getActivity(), Report_List.class);
                    startActivity(intent);
                }
            });

//            cbutton = (Button)getActivity().findViewById(R.id.button);
           mSearchView = (SearchView)getActivity().findViewById(R.id.searchView);
//            cbutton.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View arg0) {
//                    // TODO Auto-generated method stub
//                    Toast.makeText(getActivity(), "success", Toast.LENGTH_LONG).show();
//                }
//            });
            lListView = (ListView) getActivity().findViewById(R.id.listView);
            lListView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mStrs));
            lListView.setTextFilterEnabled(true);

            // 设置搜索文本监听
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// 当点击搜索按钮时触发该方法
                @Override
                public boolean onQueryTextSubmit(String query) {
                    Intent intent=new Intent(getActivity(), Medicine_List.class);
                    intent.putExtra("medicine_title",query);
                    startActivity(intent);
                    return false;
                }

                // 当搜索内容改变时触发该方法
                @Override
                public boolean onQueryTextChange(String newText) {
                    if (!TextUtils.isEmpty(newText)){
                        lListView.setVisibility(View.VISIBLE);
                        lListView.setFilterText(newText);
                    }else{
                        lListView.setVisibility(View.GONE);
                        lListView.clearTextFilter();
                    }
                    return false;
                }
            });

            camera=(ImageView)getActivity().findViewById(R.id.cameraIcon);
            camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent camera=new Intent(getActivity(), Report.class);
                    startActivity(camera);
                }
            });






        }
    void setList() throws IOException {
        RequestHelper req = new RequestHelper();
        String json = req.friend_get(app.getUrl()+"medicine/all");
        Gson gson = new Gson();
        //把JSON数据转化为对象
        MedicineList medicine = gson.fromJson(json, MedicineList.class);
        medicinelist = medicine.getMedicines();
        list_map = new ArrayList<Map<String,String>>();
        for (int i=0;i<medicinelist.size();i++)
        {
            Map<String,String> items = new HashMap<String, String>(); //创建一个键值对的Map集合
            items.put("name",medicinelist.get(i).getMedicine_name());
            items.put("waring",medicinelist.get(i).getWarning());
            list_map.add(items);   //把这个存放好数据的Map集合放入到list中，这就完成类数据源的准备工作
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(getActivity(),list_map,R.layout.waring_item,new String[]{"name","waring"},new int[]{R.id.medicine_name,R.id.waring});
        medicineList.setAdapter(simpleAdapter);
        medicineList.setTextFilterEnabled(true);
        medicineList.setVisibility(View.VISIBLE);
    }

}
